package com.finchina.myview.androidviewhelpers;

import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * <b>Project:</b> My View<br>
 * <b>Create Date:</b> 2016/1/20<br>
 * <b>Author:</b> huang saisai<br>
 * <b>Description:</b> <br>
 */
public class CustomViewScrollers extends LinearLayout {

    private   static   final  String TAG =  "Scroller" ;

    private Scroller mScroller;

    public  CustomViewScrollers(Context context, AttributeSet attrs) {
        super (context, attrs);
        mScroller =  new  Scroller(context, new LinearInterpolator());
        //LinearInterpolator
    }

    //调用此方法滚动到目标位置
    public   void  smoothScrollTo( int  fx,  int  fy) {
        int  dx = fx - mScroller.getFinalX();
        int  dy = fy - mScroller.getFinalY();
        smoothScrollBy(dx, dy);
    }

    //调用此方法设置滚动的相对偏移
    public   void  smoothScrollBy( int  dx,  int  dy) {

        //设置mScroller的滚动偏移量
        mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy);
        invalidate();
    }

    @Override
    public   void  computeScroll() {

        //先判断mScroller滚动是否完成
        if  (mScroller.computeScrollOffset()) {

            //这里调用View的scrollTo()完成实际的滚动
            this.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

            //必须调用该方法，否则不一定能看到滚动效果
            postInvalidate();
        }
//        super .computeScroll();
    }

/*    // layout过程
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
//        Log.i(TAG, "--- start onLayout --");
        int startLeft = 0; // 每个子视图的起始布局坐标
        int startTop = 0; // 间距设置为10px 相当于 android：marginTop= "10px"
        int childCount = getChildCount();
//        Log.i(TAG, "--- onLayout childCount is -->" + childCount);
        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            child.layout(mScroller.getCurrX(), mScroller.getCurrY(),
                    mScroller.getCurrX() + child.getMeasuredWidth(),
                    mScroller.getCurrY() + child.getMeasuredHeight());
//            startLeft = startLeft + 30 ; //校准每个子View的起始布局位置
            //三个子视图的在屏幕中的分布如下 [0 , 320] / [320,640] / [640,960]
        }

    }*/


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }
}
